from django.shortcuts import render, get_object_or_404
from django.views.decorators.http import require_GET
from django.views.decorators.csrf import csrf_exempt
import os

from django.conf import settings
if settings.DATABASE_TYPE == 'mysql':
    from tanxi.models.mysql.mysql_user_model import UserModel, UserManager 
    from tanxi.models.mysql.mysql_article_model import ArticleModel
        
elif settings.DATABASE_TYPE == 'sqlite':
    from tanxi.models.sqlite.sqlite_user_model import SqliteUserModel as UserModel
    from tanxi.models.sqlite.sqlite_user_model import SqliteUserManager as UserManager
    from tanxi.models.sqlite.sqlite_article_model import SqliteArticleModel as ArticleModel
else:
    raise ValueError("Unsupported database type specified in settings.")



html_path =  os.path.join(os.path.dirname(__file__), '..', '..','..' 'templates/')

@require_GET
@csrf_exempt
def home(request):
    return render(request, 'index.html')

@require_GET
@csrf_exempt
def article_detail(request, article_id):
    from tanxi.client.Handler.JsonResponse import create_response
    from tanxi.client.utils.sqlite.articles_sqlite import ArticleQueryHandler
    
    # 使用ArticleQueryHandler来获取文章详情
    article = ArticleQueryHandler().query_articles(id=article_id, is_published=True)
    if not article:
        return create_response(code=404, message="Error", remarks="文章不存在", data=None)
    
    return create_response(code=200, message="Success", remarks="获取文章详情成功", data=list(article))